#include "bool.h"

#include <QtCore/QRegExp>
#include <QtCore/QVariant>

using namespace core;

const TypeName Bool::NAME = Type::demangledName(typeid(Bool).name());

Bool::Bool(bool value) : ValueType(),
    m_value(value)
{
    m_operations.insert(OPERATION_EQUALTO, OP(NAME));
    m_operations.insert(OPERATION_NEQUALTO, OP(NAME));
}

Bool::~Bool()
{

}

void Bool::setValue(const QVariant& value)
{
    if (value.canConvert(QVariant::Bool))
    {
        m_value = value.toBool();
    }
}

QVariant Bool::getValue() const
{
    return QVariant(m_value);
}

bool Bool::operator ==(const Type& type) const
{
    const Bool* other = dynamic_cast<const Bool *>(&type);
    return other != 0 ? m_value == other->m_value : false;
}

bool Bool::operator !=(const Type& type) const
{
    const Bool* other = dynamic_cast<const Bool *>(&type);
    return other != 0 ? m_value != other->m_value : true;
}

bool Bool::canConvert(const TypeValue& value) const
{
    QRegExp regex(keywordMask());
    QVariant variant(value);

    return regex.exactMatch(value) && variant.canConvert(QVariant::Bool);
}

ValueKeywordMask Bool::keywordMask() const
{
    return "(true|false)";
}

Type* Bool::copy() const
{
    Bool* newInstance = new Bool(m_value);
    return newInstance;
}
